﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;

namespace KFS_Inventory_CigInput
{
    public partial class managerAuth : Form
    {
        public Boolean ReturnValue1 { get; set; } // Check if manager is authorized
        public managerAuth()
        {
            InitializeComponent();
        }

        private void managerAuth_Load(object sender, EventArgs e)
        {

        }

        private void loginButton_Click(object sender, EventArgs e)
        {
            string username = userBox.Text;
            string password = pwBox.Text;

            // Connect to the db to see if these values match a manager's criteria
            var mongoConnect = "mongodb://root:b713eb8b@ds029811.mongolab.com:29811/kreiser_inventory";
            MongoClient client = new MongoClient(mongoConnect);
            MongoServer server = client.GetServer();
            MongoDatabase kfs_inv = server.GetDatabase("kreiser_inventory");
            var userCollection = kfs_inv.GetCollection<BsonDocument>("UserInfo");
            var andList = new List<IMongoQuery>();
            andList.Add(Query.EQ("username", username));
            andList.Add(Query.EQ("security", "Manager"));
            
            var userQuery = Query.And(andList);

            foreach (BsonDocument user in userCollection.Find(userQuery))
            {
                if (user["password"].ToString() == password)
                {
                    this.ReturnValue1 = true;
                    MessageBox.Show("Manager authorization successful.");
                    break;

                }
//                else
//                {
//                    ReturnValue1 = false;
//                }
            }
            if (!ReturnValue1)
            {
                MessageBox.Show("The username or password is incorrect");
            }
            else
            {
                this.Close();
            }

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
